package com.xf.system.controller;

import com.xf.system.common.facade.UserFacade;
import com.xf.system.http.req.user.*;
import com.xf.system.http.vo.user.UserDetailVO;
import com.xf.system.http.vo.user.UserVO;
import com.xf.system.response.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author xiefengf
 */
@Api(tags = "用户服务Api")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserFacade userFacade;

    @GetMapping("/listUsers")
    @Operation(summary = "根据条件分页查询用户列表",method = "GET")
    public ApiResult<UserVO> listUsers(@ModelAttribute UserListReqDTO reqDto) {
        UserVO vo = userFacade.listUsers(reqDto);
        return ApiResult.success(vo);
    }

    @GetMapping("/getUserById/{id}")
    @Operation(summary = "通过用户id查询用户详情信息",method = "GET")
    public ApiResult<UserDetailVO> getUserById(@PathVariable(value = "id") Long id) {
        UserDetailVO vo = userFacade.getUserById(id);
        return ApiResult.success(vo);
    }

    @PostMapping("/saveUser")
    @Operation(summary = "新增用户信息",method = "POST")
    public ApiResult<Boolean> saveUser(@RequestBody @Valid UserAddReqDTO reqDto) {
        Boolean aBoolean = userFacade.saveUser(reqDto);
        return ApiResult.success(aBoolean);
    }

    @PutMapping("/updateUser")
    @Operation(summary = "更新用户信息",method = "PUT")
    public ApiResult<Boolean> updateUser(@RequestBody @Valid UserUpdateReqDTO reqDto) {
        Boolean aBoolean = userFacade.updateUser(reqDto);
        return ApiResult.success(aBoolean);
    }

    @DeleteMapping("/deleteUser/{idList}")
    @Operation(summary = "批量删除用户信息",method = "DELETE")
    public ApiResult<Boolean> deleteUser(@PathVariable(value = "idList") Long[] idList) {
        Boolean aBoolean = userFacade.deleteUser(idList);
        return ApiResult.success(aBoolean);
    }

    @PutMapping("/resetPwd")
    @Operation(summary = "重置用户密码",method = "PUT")
    public ApiResult<Boolean> resetPwd(@RequestBody @Valid UserResetPwdReqDTO reqDto) {
        Boolean aBoolean = userFacade.resetPwd(reqDto);
        return ApiResult.success(aBoolean);
    }

    @PutMapping("/changeStatus")
    @Operation(summary = "用户状态修改",method = "PUT")
    public ApiResult<Boolean> changeStatus(@RequestBody @Valid UserChangeStatusReqDTO reqDto) {
        Boolean aBoolean = userFacade.changeStatus(reqDto);
        return ApiResult.success(aBoolean);
    }

    @PostMapping("/downloadImportTemplate")
    @ApiOperation(httpMethod = "POST",value = "下载导入模板")
    public ApiResult<Boolean> downloadImportTemplate(HttpServletResponse response){
        userFacade.downloadImportTemplate(response);
        return ApiResult.success(Boolean.TRUE);
    }

    @PostMapping("/importExcel")
    @ApiOperation(httpMethod = "POST",value = "导入数据-Excel")
    public ApiResult<Boolean> importExcel(@RequestParam MultipartFile file){
        userFacade.importExcel(file);
        return ApiResult.success(Boolean.TRUE);
    }

    @GetMapping("/exportExcel")
    @ApiOperation(httpMethod = "POST",value = "导出数据-Excel")
    public ApiResult<Boolean> exportExcel(HttpServletResponse response,@ModelAttribute UserListReqDTO reqDto){
        userFacade.exportExcel(response,reqDto);
        return ApiResult.success(Boolean.TRUE);
    }

}
